package com.theaf.core.utils;

import com.alibaba.dubbo.rpc.RpcContext;
import com.theaf.core.api.UserComService;
import com.theaf.core.api.model.Position;
import com.theaf.core.api.model.UserInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author:songwenke
 * @Description:登录信息
 * @Date:create in 9:35 2017/12/8
 * @Modified By:
 **/
public class LoginUtils
{

    public static final String DUBBO_USER_INFO = "DUBBO_USER_INFO";

    private static UserComService userComService;

    /**
     * 获取当前用户信息
     * @return
     */
    public static UserInfo getUser(){
        Subject subject = SecurityUtils.getSubject();
        UserInfo ui = (UserInfo)subject.getPrincipal();
        if(ui==null){
            ui = (UserInfo) RpcContext.getContext().get(DUBBO_USER_INFO);
        }
        if(ui==null){
            throw SmExceptionHelper.sysCheckException("未登录用户");
        }
        return ui;
    }

    /**
     * 获取当前用户账号
     * @return
     */
    public static String getUserAccount(){
        return getUser().getAccount();
    }

    /**
     * 获取用户名称
     * @return
     */
    public static String getUserName(){
        return getUser().getName();
    }

    /**
     * 获取当前用户机构名
     * @return
     */
    public static String getUserOrganName(){
        return getUser().getOrganName();
    }

    /**
     * 获取当前机构ID
     * @return
     */
    public static long getUserOrganId(){
        return getUser().getOrganId();
    }

    /**
     * 获取当前用户ID
     * @return
     */
    public static long getUserId(){
        return getUser().getId();
    }

    /**
     * 获取当前用户手机
     * @return
     */
    public static String getUserPhone(){
        return getUser().getPhone();
    }

    /**
     * 获取当前数据权限
     * @return
     */
    public static List<Long> getOrganRange(){
        if(userComService==null){
            userComService = (UserComService)SpringUtils.getDubboService(UserComService.class);
        }
        return userComService.findOrganRangeByUserId(getUserId());
    }

    /**
     * 获取当前功能权限
     * @return
     */
    public static List<Long> getFunAath(){
        if(userComService==null){
            userComService = (UserComService)SpringUtils.getDubboService(UserComService.class);
        }
        return userComService.findAuthByUserId(getUserId())
                .parallelStream().map(n->n.getId()).collect(Collectors.toList());
    }

    /**
     * 获取当前功能权限
     * @return
     */
    public static List<String> getFunAathUrl(){
        if(userComService==null){
            userComService = (UserComService)SpringUtils.getDubboService(UserComService.class);
        }
        return userComService.findAuthByUserId(getUserId())
                .parallelStream().filter(n->n.getUrl().startsWith(getUser().getSysPath()))
                .map(n->n.getUrl().replaceAll(getUser().getSysPath(),"")).collect(Collectors.toList());
    }

    /**
     * 获取所有功能资源地址
     * @return
     */
    public static List<String> getAllFunUrl(){
        if(userComService==null){
            userComService = (UserComService)SpringUtils.getDubboService(UserComService.class);
        }
        return userComService.getAllFunUrl()
                .parallelStream().filter(n->n.startsWith(getUser().getSysPath()))
                .map(n->n.replaceAll(getUser().getSysPath(),"")).collect(Collectors.toList());
    }

    /**
     * 判断功能权限
     * @param fun
     * @return
     */
    public static boolean judgeFun(String fun){
        return getFunAath().contains(fun);
    }

    /**
     * 判断岗位
     * @param posName
     * @return
     */
    public static boolean judgePos(String posName){
        Position position = getUser().getPosition();
        return position==null?false:position.getName().equals(posName);
    }
}
